<?php
 namespace Home\Controller;
 use Think\Controller;
        /**
         * 设备管理控制器
         */
        class EquipmenteController extends HomeController{

        /**
         * 设备列表
         */
        public function index()
        {

          $arg['page']=I('get.page',1,'strip_tags');
          //设备类型
          $type=M('config')->where('id=%d',2)->getField('value');
          $type=unserialize($type);

          $device=D('Device');
          $devices= $device->index($arg);

          $this->assign('type',$type);
          $this->assign('devices',$devices['devices']);
          $this->assign('pagearr',$devices['pagearr']);

          $this->display(); 
        }

        /**
         * 添加设备
         */
        public function append(){
            if(IS_POST){
             
             $data['tiypeid']=I('post.tiypeid','','strip_tags');  
             $data['devicecode']=I('post.devicecode','','strip_tags');
             $data['yid']=I('post.yunying',0,'strip_tags');
             $data['createtime']=time();
             $result=M('device')->add($data);
             if(!empty($result))
             {
                $this->success('添加设备成功');
             }

            

          }else{
          	//获取设备类型
             $devicetype=M('config')->where('id=%d',2)->getField('value');
             $devicetype=unserialize($devicetype);
             //获取所有运营商
             $yunying=M('yunying_user')->where('isdel=0')->getField('yid,companyname');
             $this->assign('devicetype',$devicetype);
             $this->assign('yunying',$yunying);
             $this->display();

          }


        }



        /**
         * 检测设备唯一
         */
        public function weiyi()
        {
           $where['devicecode']=$_GET['re'];
           $re=M('device')->where($where)->select();
           if(empty($re)){
             $data = array('status'=>200,'code'=>'success','info'=>'无效的手机号');
             echo json_encode($data);
           }else{
             $data = array('status'=>404,'code'=>'error','info'=>'该标识已存在');
             echo json_encode($data);
           }

        }



        /**
        * 冻结设备
        */
        public function frozen()
        {
          $where['shopid']=$_GET['id'];
          $data['isdel']=1;
          M('shop_user')->where($where)->save($data);
          $this->display("Equipmente/index");
        }


        /**
        * 设备编辑
        */
        public function edit(){
            if(IS_POST)
            {
                         

            $data['tiypeid']=I('post.tiypeid','','strip_tags');

            $data['devicecode']=I('post.devicecode','','strip_tags');
            $data['yid']=I('post.yid',0,'strip_tags');
            $where['deviceid']=I('post.deviceid',0,'strip_tags');

            $re=M('device')->where($where)->save($data);

                      
            if($res ===false){
                 echo "<script type='text/javascript'>alert('修改失败'); window.history.go(-1);</script>";
            }else{
               $this->redirect("Equipmente/index");
            }
            }else{
                    //设备类型
                   $type=M('config')->where('id=%d',2)->getField('value');
                   $type=unserialize($type);
                   //获取所有运营商
                   $yunying=M('yunying_user')->where('isdel=0')->getField('yid,companyname');

                   $device=M('device a');
                   $where['a.deviceid']=I('get.id',0,'strip_tags');
                   $where['a.deviceid']=intval($where['a.deviceid']);

                   $result=$device->join('left join '.C(DB_PREFIX).'yunying_user b'.' on a.yid=b.yid ')
                                  ->where($where)
                                  ->getField('a.deviceid,a.devicecode,a.tiypeid,a.yid,b.companyname');
                    $this->assign('yunying',$yunying);
                    $this->assign('type',$type);
                    $this->assign('info',$result[$where['a.deviceid']]);
                    $this->display();
                   
                }
        }

        /*
         *搜索设备
         *@param
         *
         * return json
         */
        public function searchDivece()
        {
          //接收参数
          $arg['devicecode']=I('post.devicecode','','strip_tags');
          $arg['isfen']=I('post.isfen','','strip_tags');
          $arg['isfen']=intval($arg['isfen']);
          $arg['start']=I('post.starttime','','strip_tags');
          $arg['end']=I('post.endtime','','strip_tags');
          $arg['companyname']=I('post.companyname','','strip_tags');
          $arg['tiypeid']=I('post.type',0,'strip_tags'); //设备类型
          $arg['page']=I('post.page',1,'strip_tags');
          $device=D('Device');
          $result=$device->index($arg);
          if(!empty($result['devices'])){
            $result['status']=1;
            echo json_encode($result,JSON_UNESCAPED_UNICODE);
            return '';

          }

          $result['status']=0;
          $result['message']='暂无数据';
          echo  json_encode($result,JSON_UNESCAPED_UNICODE);



          
        } 
        


}